local n $n
local dataspec $dataspec
local bs = $bs

use "$tempfold/$dataspec", clear

xtset  co_code1 year, yearly
destring(nic_08_2dig), g(nic2)

gen diff_ln_IND = ln_IND - L.ln_IND
gen diff_ln_IVS = ln_IVS - L.ln_IVS
gen diff_tfpr = tfpr - L.tfpr

foreach var in wIPT_IND_CHN wIPT_LMI_CHN_EXCL_IND rwIPT_IND_CHN rwIPT_LMI_CHN_EXCL_IND {
	generate ln_`var' = ln(`var')
	generate diff_`var' = ln_`var' - L.ln_`var'
}

foreach v in diff_tfpr diff_ln_IND diff_ln_IVS {
	drop if missing(`v')
}

** To get the final sample for all regs:
	ivreghdfe diff_tfpr ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl , a(nic2#year) first cluster(nic_08_4dig)
			generate sample0 = e(sample)
			keep if sample0 == 1

merge 1:m  co_code1 year using "$tempfold/bssample2"
keep if _merge==3
drop _merge

foreach var in diff_tfpr  {
preserve
* Baseline IV 		
	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(year) first cluster(nic_08_4dig)
				local b`n'_y = _b[diff_ln_IND]
				local se_b`n'_y = _se[diff_ln_IND]
				cap local I_b2_y = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_y = _se[diff_wIPT_IND_CHN]
				cap local I_b3_y = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_y = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_y = e(N)
				mat f = e(first)
				local F_b`n'_y = round(f[8,1], .01)
				cap local F_I_b`n'_y = round(f[8,2], .01)
				

	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(year nic2) first cluster(nic_08_4dig)
				local b`n'_yn = _b[diff_ln_IND]
				local se_b`n'_yn = _se[diff_ln_IND]
				cap local I_b2_yn = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_yn = _se[diff_wIPT_IND_CHN]
				cap local I_b3_yn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_yn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_yn = e(N)
				mat f = e(first)
				local F_b`n'_yn = round(f[8,1], .01)
				cap local F_I_b`n'_yn = round(f[8,2], .01)

	ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(nic2#year) first cluster(nic_08_4dig)
				local b`n'_ycn = _b[diff_ln_IND]
				local se_b`n'_ycn = _se[diff_ln_IND]
				cap local I_b2_ycn = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_ycn = _se[diff_wIPT_IND_CHN]
				cap local I_b3_ycn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_ycn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_ycn = e(N)
				mat f = e(first)
				local F_b`n'_ycn = round(f[8,1], .01)
				cap local F_I_b`n'_ycn = round(f[8,2], .01)
		
* Baseline OLS
	* Year FE
	reghdfe `var' $rhs1 $rhs2 $ctrl , a(year)  cluster(nic_08_4dig)
				local Ob`n'_y = _b[diff_ln_IND]
				local se_Ob`n'_y = _se[diff_ln_IND]
				cap local OI_b2_y = _b[diff_wIPT_IND_CHN]
				cap local se_OI_b2_y = _se[diff_wIPT_IND_CHN]
				cap local OI_b3_y = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_OI_b3_y = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local ON`n'_y = e(N)
			
	reghdfe `var' $rhs1 $rhs2 $ctrl , a(year nic2)  cluster(nic_08_4dig)
				local Ob`n'_yn = _b[diff_ln_IND]
				local se_Ob`n'_yn = _se[diff_ln_IND]
				cap local OI_b2_yn = _b[diff_wIPT_IND_CHN]
				cap local se_OI_b2_yn = _se[diff_wIPT_IND_CHN]
				cap local OI_b3_yn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_OI_b3_yn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local ON`n'_yn = e(N)
		
	reghdfe `var' $rhs1 $rhs2 $ctrl , a(nic2#year)  cluster(nic_08_4dig)
				local Ob`n'_ycn = _b[diff_ln_IND]
				local se_Ob`n'_ycn = _se[diff_ln_IND]
				cap local OI_b2_ycn = _b[diff_wIPT_IND_CHN]
				cap local se_OI_b2_ycn = _se[diff_wIPT_IND_CHN]
				cap local OI_b3_ycn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_OI_b3_ycn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local ON`n'_ycn = e(N)
			
* Reduced Form		
	reghdfe `var' $ivs1 $ivs2 $ctrl , a(year)  cluster(nic_08_4dig)
				local Rb`n'_y = _b[diff_ln_IVS]
				local se_Rb`n'_y = _se[diff_ln_IVS]
				cap local RI_b2_y = _b[diff_wIPT_IND_CHN]
				cap local se_RI_b2_y = _se[diff_wIPT_IND_CHN]
				cap local RI_b3_y = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b3_y = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local RN`n'_y = e(N)
	
	reghdfe `var' $ivs1 $ivs2 $ctrl , a(year nic2)  cluster(nic_08_4dig)
				local Rb`n'_yn = _b[diff_ln_IVS]
				local se_Rb`n'_yn = _se[diff_ln_IVS]
				cap local RI_b2_yn = _b[diff_wIPT_IND_CHN]
				cap local se_RI_b2_yn = _se[diff_wIPT_IND_CHN]
				cap local RI_b3_yn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b3_yn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local RN`n'_yn = e(N)

	reghdfe `var' $ivs1 $ivs2 $ctrl , a(nic2#year)  cluster(nic_08_4dig)
				local Rb`n'_ycn = _b[diff_ln_IVS]
				local se_Rb`n'_ycn = _se[diff_ln_IVS]
				cap local RI_b2_ycn = _b[diff_wIPT_IND_CHN]
				cap local se_RI_b2_ycn = _se[diff_wIPT_IND_CHN]
				cap local RI_b3_ycn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_RI_b3_ycn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local RN`n'_ycn = e(N)

***
clear
set obs 1
gen bs = `bs'

gen `var'_b`n'_y = `b`n'_y'
cap gen `var'_I_b2_y = `I_b2_y'
cap gen `var'_I_b3_y = `I_b3_y'
gen `var'_N`n'_y = `N`n'_y'
gen `var'_F_b`n'_y = `F_b`n'_y'
cap gen `var'_F_I_b`n'_y = `F_I_b`n'_y'

gen `var'_b`n'_yn = `b`n'_yn'
cap gen `var'_I_b2_yn = `I_b2_yn'
cap gen `var'_I_b3_yn = `I_b3_yn'
gen `var'_N`n'_yn = `N`n'_yn'
gen `var'_F_b`n'_yn = `F_b`n'_yn'
cap gen `var'_F_I_b`n'_yn = `F_I_b`n'_yn'

gen `var'_b`n'_ycn = `b`n'_ycn'
cap gen `var'_I_b2_ycn = `I_b2_ycn'
cap gen `var'_I_b3_ycn = `I_b3_ycn'
gen `var'_N`n'_ycn = `N`n'_ycn'
gen `var'_F_b`n'_ycn = `F_b`n'_ycn'
cap gen `var'_F_I_b`n'_ycn = `F_I_b`n'_ycn'

gen `var'_Ob`n'_y = `Ob`n'_y'
cap gen `var'_OI_b2_y = `OI_b2_y'
cap gen `var'_OI_b3_y = `OI_b3_y'
gen `var'_ON`n'_y = `ON`n'_y'

gen `var'_Ob`n'_yn = `Ob`n'_yn'
cap gen `var'_OI_b2_yn = `OI_b2_yn'
cap gen `var'_OI_b3_yn = `OI_b3_yn'
gen `var'_ON`n'_yn = `ON`n'_yn'

gen `var'_Ob`n'_ycn = `Ob`n'_ycn'
cap gen `var'_OI_b2_ycn = `OI_b2_ycn'
cap gen `var'_OI_b3_ycn = `OI_b3_ycn'
gen `var'_ON`n'_ycn = `ON`n'_ycn'

gen `var'_Rb`n'_y = `Rb`n'_y'
cap gen `var'_RI_b2_y = `RI_b2_y'
cap gen `var'_RI_b3_y = `RI_b3_y'
gen `var'_RN`n'_y = `RN`n'_y'

gen `var'_Rb`n'_yn = `Rb`n'_yn'
cap gen `var'_RI_b2_yn = `RI_b2_yn'
cap gen `var'_RI_b3_yn = `RI_b3_yn'
gen `var'_RN`n'_yn = `RN`n'_yn'

gen `var'_Rb`n'_ycn = `Rb`n'_ycn'
cap gen `var'_RI_b2_ycn = `RI_b2_ycn'
cap gen `var'_RI_b3_ycn = `RI_b3_ycn'
gen `var'_RN`n'_ycn = `RN`n'_ycn'

save "$tempfold/bs`n'_`dataspec'_`var'", replace

restore
}

*** Compile results:

use "$tempfold/bs`n'_`dataspec'_diff_tfpr", clear
order bs
save "$tempfold/bs`n'_`dataspec'", replace
